package org.ljy.transdemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.ljy.transdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private DataSourceTransactionManager transactionManager;//用于回滚和提交事务
    @Autowired
    private TransactionDefinition transactionDefinition;//用于开启事务

    @Autowired
    private UserService userService;
    @RequestMapping("/registry")
    public Boolean registry(String name,String password) {
        //1.手动开启事务
        TransactionStatus def = transactionManager.getTransaction(transactionDefinition);

        Integer result = userService.insert(name,password);
        log.info("影响的行数：result:{}",result);

        //2.回滚事务
        transactionManager.rollback(def);
        //2.提交事务
        transactionManager.commit(def);

        return true;
    }
}
